﻿using Infrastructure.Dao;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DormMS.IService.Common;

namespace DormMS.Service.Common
{
    public class LogService:ILogService
    {
        /// <summary>
        ///  操作日志
        /// </summary>
        public int OperationLog(int userId, string userName, string url, string eventName, string describe, string ip)
        {
            string sql =
                $@"INSERT INTO [dbo].[Sys_OperationLog]([UserId],[UserName],[Url],[EventName],[Describe],[IP],[RecordTime])
                    VALUES(@UserId,@UserName,@Url ,@EventName,@Describe,@IP,@RecordTime)";
            SqlParameter[] pars =
            {
                new SqlParameter("@UserId", userId),
                new SqlParameter("@UserName", userName),
                new SqlParameter("@Url", url),
                new SqlParameter("@EventName", eventName),
                new SqlParameter("@Describe", describe),
                new SqlParameter("@IP", ip),
                new SqlParameter("@RecordTime", DateTime.Now)
            };
            using (var db = DbConfig.GetInstance())
            {
                //日志记录件事件里面用到数据库操作 IsEnableLogEvent一定要为false否则将引起死循环，并且要新开一个数据实例 像我这样写就没问题。
                db.IsEnableLogEvent = false;
                return db.ExecuteCommand(sql, pars);
            }
        }

        /// <summary>
        ///     安全日志
        /// </summary>
        public int SecurityLog(int userId, string userName, string url, string eventName, string describe, string ip)
        {
            string sql =
                $@"INSERT INTO [dbo].[Sys_SecurityLog]([UserId],[UserName],[Url],[EventName],[Describe],[IP],[RecordTime])
                    VALUES(@UserId,@UserName,@Url ,@EventName,@Describe,@IP,@RecordTime)";
            SqlParameter[] pars =
            {
                new SqlParameter("@UserId", userId),
                new SqlParameter("@UserName", userName),
                new SqlParameter("@Url", url),
                new SqlParameter("@EventName", eventName),
                new SqlParameter("@Describe", describe),
                new SqlParameter("@IP", ip),
                new SqlParameter("@RecordTime", DateTime.Now)
            };
            using (var db = DbConfig.GetInstance())
            {
                //日志记录件事件里面用到数据库操作 IsEnableLogEvent一定要为false否则将引起死循环，并且要新开一个数据实例 像我这样写就没问题。
                db.IsEnableLogEvent = false;
                return db.ExecuteCommand(sql, pars);
            }
        }
    }
}
